package com.dongdongshop.service.impl.item_cat;

import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.mapper.TbTypeTemplateMapper;
import com.dongdongshop.pojo.itemCat.TbItemCat;
import com.dongdongshop.pojo.type_template.TbTypeTemplate;
import com.dongdongshop.service.itemCat.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    @Override
    public List<TbItemCat> listParent(Long parentId) {
      /*  TbItemCat tbItemCat = tbItemCatMapper.selectByParentId(parentId);*/
        List<TbItemCat> list = new ArrayList<>();
        List<TbItemCat> listParent = getListParent(list,parentId);
        list.add(new TbItemCat(0L,null,"顶级分类列表",null));
        return list;
    }

    @Override
    public List<TbItemCat> listItemCat(Long parentId) {
        return tbItemCatMapper.listItemCatByParentId(parentId);
    }

    @Override
    @Transactional
    public int insertItemCat(TbItemCat tbItemCat) {
        return tbItemCatMapper.insertSelective(tbItemCat);
    }

    @Override
    public TbItemCat getItemCat(Long id) {
        return tbItemCatMapper.selectByParentId(id);
    }

    @Override
    @Transactional
    public int updateItemCat(TbItemCat tbItemCat) {
        return tbItemCatMapper.updateByPrimaryKeySelective(tbItemCat);
    }

    @Override
    public int deleteItemCatBatch(Long[] ids) {
        return tbItemCatMapper.deleteItemCatBatch(ids);
    }

    @Override
    public TbItemCat getItemCatById(Long id) {
        return tbItemCatMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TbItemCat> listCategory(Long parentId) {
        List<TbItemCat> list = new ArrayList<>();
        List<TbItemCat> listParent = getListParent(list,parentId);
        return listParent;
    }

    private List<TbItemCat> getListParent(List<TbItemCat> list,Long parentId){
         if (parentId!=0L){
             TbItemCat tbItemCat = tbItemCatMapper.selectByParentId(parentId);
             list.add(tbItemCat);
             getListParent(list,tbItemCat.getParentId());
         }
         return list;
     }
}
